﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class lensPriceManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            PanelInput.Visible = false;
            binddataBrandName();
            binddataLensName();
        }
    }

    private void binddataBrandName()
    {
        DropDownListBrand.DataSource = CSUtils.getAllBrandList();
        DropDownListBrand.DataBind();
    }

    private void binddataLensName()
    {
        DropDownListLens.DataSource = CSUtils.getAllLensList();
        DropDownListLens.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e) {
        PanelInput.Visible = false;
        PanelRef.Visible = false;
        this.binddata();
    }

    private void binddata() {
        string brandID = DropDownListBrand.SelectedValue;
        string lensID = DropDownListLens.SelectedValue;
        GridView1.DataSource = this.getAllLensPrice(brandID, lensID);
        GridView1.DataBind();
        if (!lensID.Equals("-1")) {
            PanelAction.Visible = true;
        }
        else {
            PanelAction.Visible = false;
        }
    }


    private DataSet getAllLensPrice(string brandID, string lensID)
    {
        string sql =
            "select price.id, price.brand_id, price.lens_id, price.sphere_begin, price.sphere_end, price.cylinder_begin, price.cylinder_end, price.diameter, price.sale_price, price.agent_price, price.cost1, price.cost2, price.cost3, price.cost4, price.cost5," +            
            "lens_name, brand_name " +
            "from cs_lens_price_base price " +
            "left join cs_lens_base lens on price.lens_id = lens.lens_id " +
            "left join cs_brands brands on price.brand_id = brands.brand_id " +
            "where 1= 1 {0}" +
            "order by brand_name, lens_name, sphere_begin,sphere_end,cylinder_begin,cylinder_end";

        string where = "";

        if (!brandID.Equals("-1"))
        {
            where += " and price.brand_id = " + brandID;
        }

        if (!lensID.Equals("-1"))
        {
            where += " and price.lens_id = " + lensID;
        }
        
        sql = string.Format(sql, where);

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {



        if (e.CommandName.Equals("command_del"))
        {
            string id = e.CommandArgument.ToString();

            if (this.deleteLensPrice(id))
            {
            }
            else
            {
                JScript.Alert("删除失败！");
            }

            this.binddata();


        }
        else if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add"))
        {

            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;
            LabelID.Text = e.CommandArgument.ToString();
            LensPriceVO vo = CSUtils.getLensPrice(LabelID.Text);
            LabelLensName.Text = vo.Name;
            DropDownListSphereBegin.SelectedValue = vo.SphereBegin;
            DropDownListSphereEnd.SelectedValue = vo.SphereEnd;
            DropDownListCylinderBegin.SelectedValue = vo.CylinderBegin;
            DropDownListCylinderEnd.SelectedValue = vo.CylinderEnd;
            TextBoxDiameter.Text = vo.Diameter;
            TextBoxSalePrice.Text = vo.SalePrice;
            TextBoxAgentPrice.Text = vo.AgentPrice;

        }
        else
        {
            this.cleanPanelInput();
        }
        

    }


    protected void ButtonSubmit_Click(object sender, EventArgs e) {

        string id = LabelID.Text;
        string lensID = DropDownListLens.SelectedValue;
        string brandID = DropDownListBrand.SelectedValue;
        string sphereBegin = DropDownListSphereBegin.SelectedValue;
        string sphereEnd = DropDownListSphereEnd.SelectedValue;
        string cylinderBegin = DropDownListCylinderBegin.SelectedValue;
        string cylinderEnd = DropDownListCylinderEnd.SelectedValue;
        string diameter = TextBoxDiameter.Text;
        string salePrice = TextBoxSalePrice.Text;
        string agentPrice = TextBoxAgentPrice.Text;

        //do some check
        float begin = float.Parse(sphereBegin);
        float end = float.Parse(sphereEnd);
        if (begin != 0.00 || end != 0.00)
        {
            if (begin >= end)
            {
                JScript.Alert("非法输入：(球径) 开始必须小于结束！");
                return;
            }
        }

        begin = float.Parse(cylinderBegin);
        end = float.Parse(cylinderEnd);
        if (begin != 0.00 || end != 0.00)
        {
            if (begin >= end)
            {
                JScript.Alert("非法输入：(柱径) 开始必须小于结束！");
                return;
            }
        }



        string commandType = TextBoxCommandType.Text;
        LensPriceVO vo = null;
                
        if (commandType.Equals("command_edit"))
        {
            //get vo from db
            vo = CSUtils.getLensPrice(id);

            //update there info
            vo.SphereBegin = sphereBegin;
            vo.SphereEnd = sphereEnd;
            vo.CylinderBegin = cylinderBegin;
            vo.CylinderEnd = cylinderEnd;
            vo.Diameter = diameter;
            vo.SalePrice = salePrice;
            vo.AgentPrice = agentPrice;
            vo.Cost1 = "0";
            vo.Cost2 = "0";
            vo.Cost3 = "0";
            vo.Cost4 = "0";
            vo.Cost5 = "0";
        }
        else if (commandType.Equals("command_add"))
        {
            vo = new LensPriceVO();

            vo.LensID = lensID;
            vo.BrandID = brandID;
            vo.SphereBegin = sphereBegin;
            vo.SphereEnd = sphereEnd;
            vo.CylinderBegin = cylinderBegin;
            vo.CylinderEnd = cylinderEnd;
            vo.Diameter = diameter;
            vo.SalePrice = salePrice;
            vo.AgentPrice = agentPrice;
            vo.Cost1 = "0";
            vo.Cost2 = "0";
            vo.Cost3 = "0";
            vo.Cost4 = "0";
            vo.Cost5 = "0"; 
        }


        if (CSUtils.mergeLensPrice(vo))
        {
            //JScript.Alert("保存成功！");
            TextBoxDiameter.Text = "";
            TextBoxSalePrice.Text = "";
            TextBoxAgentPrice.Text = "";
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();

    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e) {
        if (DropDownListLens.SelectedValue.Equals("-1"))
        {
            return;
        }
        PanelRef.Visible = false;
        PanelInput.Visible = true;
        TextBoxCommandType.Text = "command_add";
        this.cleanPanelInput();
        LabelLensName.Text = DropDownListLens.SelectedItem.Text;

    }


    private void cleanPanelInput() {
        LabelID.Text = "";
        LabelLensName.Text = "";
        DropDownListSphereBegin.SelectedValue = "0.00";
        DropDownListSphereEnd.SelectedValue = "0.00";
        DropDownListCylinderBegin.SelectedValue = "0.00";
        DropDownListCylinderEnd.SelectedValue = "0.00";
        TextBoxDiameter.Text = "";
        TextBoxSalePrice.Text = "";
        TextBoxAgentPrice.Text = "";
    }


    private bool deleteLensPrice(string id)
    {

        string sqlDel =
            "delete cs_lens_price_base " +
            "where id = " + id;

        DBConn myDB = new DBConn();
        int tmp = myDB.Delete(sqlDel);
        myDB.Close();
        return (tmp == 1);

    }

    protected void LinkButtonRef_Click(object sender, EventArgs e) {
        PanelInput.Visible = false;        
        PanelRef.Visible = true;
        DropDownListRef_Brand.DataSource = this.getRefBrand();
        DropDownListRef_Brand.DataBind();
        DropDownListRef_Lens.DataSource = this.getRefLens(DropDownListRef_Brand.SelectedValue);
        DropDownListRef_Lens.DataBind();
    }

    private DataSet getRefBrand()
    {
        string lensID = DropDownListLens.SelectedValue;
        string sql =
            "select distinct b.brand_id, c.brand_name from cs_lens_price_base b " +
            "left join cs_brands c on b.brand_id = c.brand_id " +
            "order by b.brand_id";

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    private DataSet getRefLens(string brandID)
    {
        string lensID = DropDownListLens.SelectedValue;
        string sql =
            "select distinct b.lens_id, c.lens_name from cs_lens_price_base b " +
            "left join cs_lens_base c on b.lens_id = c.lens_id " +
            "where b.brand_id = " +  brandID + " " +
            "order by c.lens_name";

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void ButtonRef_Click(object sender, EventArgs e) {

        string brandID = DropDownListBrand.SelectedValue;
        string lensID = DropDownListLens.SelectedValue;
        string refBrandID = DropDownListRef_Brand.SelectedValue;
        string refLensID = DropDownListRef_Lens.SelectedValue;

        if (brandID == refBrandID && lensID == refLensID)
        {
            LabelErrorMsg.Text = "不能自己参照自己！";
            return;
        }

        if (this.doRef(brandID, lensID, refBrandID, refLensID))
        {
            LabelErrorMsg.Text = "";
        }
        else
        {
            LabelErrorMsg.Text = "参照失败！";
            return;
        }

        this.binddata();

    }

    private bool doRef(string brandID, string lensID, string refBrandID, string refLensID)
    {

        string sql =
            "delete from cs_lens_price_base where brand_id = {0} and lens_id = {1};" +
            "INSERT INTO cs_lens_price_base " +
            "   ([brand_id], [lens_id], [sphere_begin], [sphere_end], [cylinder_begin], [cylinder_end], [diameter], [sale_price], [agent_price], [cost1], [cost2], [cost3], [cost4], [cost5]) " +
            "   select {0}as brand_id, {1}as lens_id, [sphere_begin], [sphere_end], [cylinder_begin], [cylinder_end], [diameter], [sale_price], [agent_price], [cost1], [cost2], [cost3], [cost4], [cost5] " +
            "   from cs_lens_price_base " +
            "   where brand_id = {2} and lens_id = {3}";

        sql = string.Format(sql, brandID, lensID, refBrandID, refLensID);

        DBConn myDB = new DBConn();
        int tmp = myDB.Update(sql);
        myDB.Close();
        return (tmp > 0);
    }



    protected void DropDownListRef_Brand_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownListRef_Lens.DataSource = this.getRefLens(DropDownListRef_Brand.SelectedValue);
        DropDownListRef_Lens.DataBind();
    }
}
